package com.minelx.scriptbuilder.parse.parser;

import com.minelx.scriptbuilder.script.operation.AssignOperation2;
import com.minelx.sequence.IStringScanner;
import com.minelx.sequence.ReturnMatcher;
import com.minelx.scriptbuilder.script.IVariable;
import com.minelx.scriptbuilder.script.ScriptContext;
import com.minelx.scriptbuilder.script.operation.AssignOperation;
import com.minelx.scriptbuilder.script.operation.IOperation;

public class AssignParser implements ReturnMatcher<IOperation, IStringScanner> {

	private final ValuesParser valuesParser;

	public AssignParser(ScriptContext context) {
		valuesParser = new ValuesParser(context);
	}

	@Override
	public boolean isMatch(IStringScanner scanner) {
		IStringScanner copy = scanner.copy();
		IVariable assignment = valuesParser.parse(copy);
		if (assignment.isNull()) {
			return false;
		}
		return copy.exists("=");
	}

	@Override
	public IOperation onMatched(IStringScanner scanner) {
		IVariable assigned = valuesParser.parse(scanner);
		scanner.nextChar(); // '='
		IVariable as = valuesParser.parse(scanner);
		scanner.nextChar(); // ';'
		return new AssignOperation2(assigned, as);
	}
}
